/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.supplier.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.K100;
import queenb.net.entitybean.N100;
import queenb.web.controller.QueenbController;
import queenb.web.crm.store.CustomerStore;
import queenb.web.crm.store.K100Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.ContextUtil;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class K100partnerController extends QueenbController{
  
    private K100Store k100Store;
    private String KV104;// Nguoi gioi thieu 
    private String KV102;// 
    private String KV101;
    private String KV103;
    private String KV105;// Mã nhà cung cấp
    private int PK100;
    private int FN550;
    private int FN850;
    private int FN900;
    private List<K100> list = new ArrayList<K100>();
    private List<SelectItem> combN550 = new ArrayList<SelectItem>();
    private List<SelectItem> selectItemN900 = new ArrayList<SelectItem>();
    private List<SelectItem> selectCombN850 = new ArrayList<SelectItem>();
    ComboboxUtil comboboxUtil = new ComboboxUtil();
    private CustomerStore customerStore;
    private String idSearch;
    private String keySearch = getResourceString("addkey");
    private int typeSearch = 0;
    private int temp=1;
    private int fk400;

    public K100partnerController() {
        //page
        getKapiMenu().setAdmin(false);
        getKapiMenu().setMainmenu1("m132"); // Hien thi Menu nao dc chon  
        System.out.println("--------------------AAAA" + getKapiMenu().getMainmenu1());
        Object ppage = getRequest().getParameter("p");
//        if (ppage != null) {
//            getPage().setCurrent(Integer.parseInt(ppage.toString()));
//        } else {
//            if (!getPage().getPagename().equals("partner.jsp")) {
//                getPage().setCurrent(1);
//            }
//        }
//        getPage().setPagename("partner.jsp");
        //page

        k100Store = new K100Store();
        searchAlpha();
              
        Object fk100 = getRequest().getParameter("pnPK100");
        if (fk100 != null) {
            this.PK100 = Util.parseInt(fk100);
            list = k100Store.getListTabK100WithID(getLogin().getUserName(), PK100);

            //getList();
            if (list != null && list.size() > 0) {
                System.out.println("--------------k100controllersave" + list.get(0).getKv103() + "-" + list.size());
                KV101 = list.get(0).getKv101();
                KV103 = list.get(0).getKv103();
                KV102 = list.get(0).getKv102();
                //  KN114 = list.get(0).getKn114();
                KV104 = list.get(0).getKv104();
                KV105 = list.get(0).getKv105();
                //  KV110 = list.get(0).getKv110();
                FN850 = list.get(0).getFn850();
                FN550 = list.get(0).getFn550();
            }
        }
        
        getKapiMenu().setAdmin(false);
        getKapiMenu().setMainmenu1("m132"); // Hien thi Menu nao dc chon   
    }

    public void reload() {
    }

    public void searchAlpha() {
        try {
            setIdSearch(ContextUtil.getRequestParameter("idSearch"));
            if (getIdSearch() != null && getIdSearch().contains("ALL")) {
                setIdSearch(null);
            }
            getLogin().setCharacter(getIdSearch());
        } catch (Exception ex) {
            setIdSearch("ALL");
        }

    }

    public List<SelectItem> getListSelectItems() {
        customerStore = new CustomerStore();
        List<N100> listN100Select = customerStore.getSelectItemN550(getLogin().getModule(), 99, 0, getLogin().getUserName());
        Iterator ic = listN100Select.iterator();
        List<SelectItem> listSelected = new ArrayList<SelectItem>();
        while (ic.hasNext()) {
            N100 n100 = (N100) ic.next();
            SelectItem item = new SelectItem(n100.getVal(), n100.getLabel());

            listSelected.add(item);
        }
        return listSelected;
    }

    public List<K100> getListK100supplierAll() {
        try {
            List<K100> listK100supplier = k100Store.getListTabK100(getLogin().getCompanyNo(), getLogin().getModule(), this.getPK100(), 4, getLogin().getCharacter(), 0, 0, 1000, 0, 1, getLogin().getUserName());
            System.out.println("===============" + listK100supplier.size());
            if (listK100supplier != null && listK100supplier.size() > 0) {
                if (listK100supplier.get(0).getRowss() != 0) {
                 //   getPage().setTotalrow(listK100supplier.get(0).getRowss());
                }

            }
            
            return listK100supplier;

        } catch (Exception ex) {
            System.out.println("===============" + ex.getMessage());
            return null;
        }
    }

    public void saveK100() {
        try {
            System.out.println("--------------k100controllersave" + KV103 + KV104 + KV101);
            K100Store store = new K100Store();
            int roweffect = 0;
            if (KV104 == null || KV104.equals("0")) {
                KV104 = getLogin().getUserName();
                System.out.println("--------------k100controllersave" + KV103 + KV104 + KV101);
            }
            if (KV102 == null || KV102.equals("0")) {
                KV102 = getLogin().getUserName();
            }
            //roweffect = store.updateK100By(0, 7, 1, 99, 1, 0, 0, 0, 0, null, null, null, null, null, null, null, null, null, null, null, null, 0, 0, "admin@sgt.com")
//            roweffect = store.updateK100By(
//                    this.PK100, getLogin().getModule(), 0, getLogin().getCompanyNo(), 1, "CC", 0, 0,
//                    4,0, this.KV101, this.KV102, this.KV103, this.KV104, this.KV105, null, null, null,
//                    null, null, null, null, 0, 0, 0, this.FN550, getLogin().getUserName());
//            if (roweffect > 0) {
//                showGlobalReport(true, getResourceString("datasavesuccessful"));
//            }
            sendRedirect("partner.jsp");
        } catch (Exception e) {
            System.out.println("-------------------------------"+ e.getMessage());
        }

    }

    public void changeCombN850forN900(ActionEvent ac) {
        selectItemN900 = comboboxUtil.getSelectedN900Comb(FN850);
        System.out.println("---change" + FN850 + "--" + selectItemN900.size());
    }

    /**
     * @return the KV104
     */
    public String getKV104() {
        return KV104;
    }

    /**
     * @param KV104 the KV104 to set
     */
    public void setKV104(String KV104) {
        this.KV104 = KV104;
    }

    /**
     * @return the KV102
     */
    public String getKV102() {
        return KV102;
    }

    /**
     * @param KV102 the KV102 to set
     */
    public void setKV102(String KV102) {
        this.KV102 = KV102;
    }

    /**
     * @return the KV101
     */
    public String getKV101() {
        return KV101;
    }

    /**
     * @param KV101 the KV101 to set
     */
    public void setKV101(String KV101) {
        this.KV101 = KV101;
    }

    /**
     * @return the KV103
     */
    public String getKV103() {
        return KV103;
    }

    /**
     * @param KV103 the KV103 to set
     */
    public void setKV103(String KV103) {
        this.KV103 = KV103;
    }

    /**
     * @return the PK100
     */
    public int getPK100() {
        return PK100;
    }

    /**
     * @param PK100 the PK100 to set
     */
    public void setPK100(int PK100) {
        this.PK100 = PK100;
    }

    /**
     * @return the FN550
     */
    public int getFN550() {
        return FN550;
    }

    /**
     * @param FN550 the FN550 to set
     */
    public void setFN550(int FN550) {
        this.FN550 = FN550;
    }

    /**
     * @return the combN550
     */
    public List<SelectItem> getCombN550() {
        combN550 = comboboxUtil.getSelectedN550Comb();
        return combN550;
    }

    /**
     * @param combN550 the combN550 to set
     */
    public void setCombN550(List<SelectItem> combN550) {
        this.combN550 = combN550;
    }

    /**
     * @return the FN850
     */
    public int getFN850() {
        return FN850;
    }

    /**
     * @param FN850 the FN850 to set
     */
    public void setFN850(int FN850) {
        this.FN850 = FN850;
    }

    /**
     * @return the FN900
     */
    public int getFN900() {
        return FN900;
    }

    /**
     * @param FN900 the FN900 to set
     */
    public void setFN900(int FN900) {
        this.FN900 = FN900;
    }

    /**
     * @return the selectItemN900
     */
    public List<SelectItem> getSelectItemN900() {
        return selectItemN900;
    }

    /**
     * @param selectItemN900 the selectItemN900 to set
     */
    public void setSelectItemN900(List<SelectItem> selectItemN900) {
        this.selectItemN900 = selectItemN900;
    }

    /**
     * @return the selectCombN850
     */
    public List<SelectItem> getSelectCombN850() {
        selectCombN850 = comboboxUtil.getSelectedN850Comb();
        return selectCombN850;
    }

    /**
     * @param selectCombN850 the selectCombN850 to set
     */
    public void setSelectCombN850(List<SelectItem> selectCombN850) {
        this.selectCombN850 = selectCombN850;
    }

    /**
     * @return the KV105
     */
    public String getKV105() {
        return KV105;
    }

    /**
     * @param KV105 the KV105 to set
     */
    public void setKV105(String KV105) {
        this.KV105 = KV105;
    }

    /**
     * @return the idSearch
     */
    public String getIdSearch() {
        return idSearch;
    }

    /**
     * @param idSearch the idSearch to set
     */
    public void setIdSearch(String idSearch) {
        this.idSearch = idSearch;
    }

    /**
     * @return the keySearch
     */
    public String getKeySearch() {
        return keySearch;
    }

    /**
     * @param keySearch the keySearch to set
     */
    public void setKeySearch(String keySearch) {
        this.keySearch = keySearch;
    }

    /**
     * @return the typeSearch
     */
    public int getTypeSearch() {
        return typeSearch;
    }

    /**
     * @param typeSearch the typeSearch to set
     */
    public void setTypeSearch(int typeSearch) {
        this.typeSearch = typeSearch;
    }

    /**
     * @return the temp
     */
    public int getTemp() {
        return temp;
    }

    /**
     * @param temp the temp to set
     */
    public void setTemp(int temp) {
        this.temp = temp;
    }

    /**
     * @return the fk400
     */
    public int getFk400() {
        return fk400;
    }

    /**
     * @param fk400 the fk400 to set
     */
    public void setFk400(int fk400) {
        this.fk400 = fk400;
    }
    
}
